<?php
/*
 * @Author: Shawn Zhang 
 * @Date: 2019-08-21 09:27:41 
 * @Last Modified by: Shawn Zhang
 * @Last Modified time: 2019-08-21 18:57:10
 * @use-for: 用于使用SkinRun的肤质检测的Api进行肤质检测
 */
namespace App\Services\Utils;
use App\Services\Utils\OSSable;
use App\Services\Utils\Transaction;

class SkinRunSkinDetect
{
    use OSSable;
    /**
     * 请求Api服务所需的token值,在ENV中使用SKINRUN_TOKEN配置
     */
    private $token;
    /**
     * 请求Api服务所需的version值,在ENV中使用SKINRUN_VERSION配置
     */
    private $version;
    /**
     * 请求Api服务所需的url值,在ENV中使用SKINRUN_URL配置
     */
    private $url;

    /**
     * 初始化SkinRunSkinDetect类
     * @param api_token SkinRun Api所需的Token
     * @param api_version SkinRun Api请求时需要提交的api的version
     * @param api_url SkinRun Api的请求地址
     */
    public function __construct($api_token, $api_version, $api_url)
    {
        $this->token =$api_token;
        $this->version =$api_version;
        $this->url =$api_url;
        // $this->token =is_null($api_token)? \env('SKINRUN_TOKEN'):$api_token;
        // $this->version =is_null($api_version) ? \env('SKINRUN_VERSION'):$api_version;
        // $this->url = is_null($api_token) ? \env('SKINRUN_URL') : $api_url;
    }

    /**
     * 使用SkinRun Api服务进行肤质检测
     * @param image 需要进行检测的图像数据
     * @param gender 检测用户的性别：1-男，2-女
     * @param birthday 检测用户的生日:2006-07-26
     * @return SkinRun Api返回的json数据
     */
    public function detect($image_url, $gender, $birthday)
    {
        /** post提交的数据 */
        // $cImg = \curl_file_create($image->getClientOriginalName(),$image->getMimeType(),'image');
        $post_data = array(
            // "image" => new \CURLFile(\realpath($image->path())),
            "image" => $image_url,
            "gender" => $gender,
            "birthday" => $birthday
        );
        /** header参数 */
        $header_data = array(
            "Content-Type: multipart/form-data",
            "token:".$this->token,
            "version:".$this->version
        );

        $ch = \curl_init();
        \curl_setopt($ch, CURLOPT_URL, $this->url);
        \curl_setopt($ch, CURLOPT_POST, 1);
        \curl_setopt($ch, CURLOPT_HEADER, 0);
        // \curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
        \curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        \curl_setopt($ch,CURLOPT_HTTPHEADER,$header_data);
        \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = \curl_exec($ch);
        \curl_close($ch);
        return $output;
    }

    /**
     * 解析图片检测的分析结果
     * @param record 肤质检测记录
     * @param data 具体采用的具体方法的肤质检测记录
     * @param language 肤质分析的结果的语言
     */
    public function analysisDetectResult($record,$data, $language='zh-CN')
    {
        $result = new \stdClass();
        $result->id = $record->id;
        $result->user_id = $record->user_id;
        $result->detect_type = $record->record_type;
        $result->created_at = $record->created_at->toDateTimeString();
        $result->image = $this->getStaticUrl($data->image);

        // 肌肤问题等级
        $result->mild = "75-100";
        $result->moderate = "41-74";
        $result->severe = "0-40";

        // 具体的肌肤问题
        // pore
        $pore = new \stdClass();
        $pore->score = (int)((1-$data->pore)*100);
        $grade = $this->checkItemGrade($pore->score);
        $pore->suggest=\trans('skinrunrecord.pore.'.$grade, $replace=[], $locale=$language);
        $result->pore =$pore;

        // wrinkle
        $wrinkle = new \stdClass();
        $wrinkle->score = (int)((1-$data->wrinkle)*100);
        $grade = $this->checkItemGrade($wrinkle->score);
        $wrinkle->suggest=\trans('skinrunrecord.wrinkle.'.$grade, $replace=[], $locale=$language);
        $result->wrinkle =$wrinkle;

        // stain
        $stain = new \stdClass();
        $stain->score = (int)((1-$data->stain)*100);
        $grade = $this->checkItemGrade($stain->score);
        $stain->suggest=\trans('skinrunrecord.stain.'.$grade, $replace=[], $locale=$language);
        $result->stain =$stain;

        // acne
        $acne = new \stdClass();
        $acne->score = (int)((1-$data->acne)*100);
        $grade = $this->checkItemGrade($acne->score);
        $acne->suggest=\trans('skinrunrecord.acne.'.$grade, $replace=[], $locale=$language);
        $result->acne =$acne;

        // eyes
        $eyes = new \stdClass();
        $eyes->score = (int)((1-$data->eyes)*100);
        $grade = $this->checkItemGrade($eyes->score);
        $eyes->suggest=\trans('skinrunrecord.eyes.'.$grade, $replace=[], $locale=$language);
        $result->eyes =$eyes;

        // oil_u
        $result->oil_u =(int)((1-$data->oil_u)*100);

        // oil_t
        $result->oil_t =(int)((1-$data->oil_t)*100);

        // 肌肤年龄
        $result->skin_age = $data->skin_age;
        // 肌肤综合得分
        $result->skin_score=$data->skin_score;
        // 肌肤类型
        $result->skin_type = $this->skinTypeTrans($data->skin_type, $language);

        return $result;
    }

    /**
     * 判断检测项的等级
     * @param $score 检测项的得分
     * @return 等级描述
     */
    private function checkItemGrade($score)
    {
        $grade="";
        if($score <= 40){
            $grade='severe';
        }elseif($score <=74){
            $grade='moderate';
        }else{
            $grade='mild';
        }
        return $grade;
    }

    /**
     * 对肤质类型进行翻译
     * @param $skin_type 肤质类型
     * @param $language 语言代码
     * @return 翻译之后的语言
     */
    private function skinTypeTrans($skin_type, $language)
    {
        $trans="";
        switch($skin_type)
        {
            case "中性":
                $trans=\trans('skinrunrecord.skin.normal', $replace=[], $locale=$language);
                break;
            case "干性":
                $trans=\trans('skinrunrecord.skin.dry', $replace=[], $locale=$language);
                break;
            case "油性":
                $trans=\trans('skinrunrecord.skin.oily', $replace=[], $locale=$language);
                break;
            case "混合性":
                $trans=\trans('skinrunrecord.skin.com', $replace=[], $locale=$language);
                break;
            default:
                $trans=\trans('skinrunrecord.skin.com', $replace=[], $locale=$language);
                break;
        }
        return $trans;
    }


}